<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/12
 * Time: 0:39
 */

namespace api\storage\controller;


use api\storage\common\Constant;
use api\storage\common\InventoryFactory;
use think\Request;

class InventoryController extends AppBaseController
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * @api {GET} /api/storage/inventory/code_check 1.检查参数
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkCode
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 按多种方式进行盘点
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Object} data
     * @apiSuccess {string} value 站位号 | 容器号 | 物料号
     * @apiError  Error
     **/
    public function checkCode()
    {
        $seatCode = input('code');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->checkParam($seatCode, $this->getStorageUuid());
        switch ($result['status']) {
            case -3:
                $this->error('正在出库请稍后盘点');
                break;
            case -11:
                $this->error('请扫描正确站位号');
                break;
            case -21:
                $this->error('请扫描正确的容器号');
                break;
            case -32:
                $this->error('请扫描正确的reelId或物料号');
                break;
            case -31:
                $this->error('仓库正在盘点其他物料请稍后');
                break;
            case 1:
                $this->success('请求成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
                break;
        }
    }

    /**
     * @api {GET} /api/storage/inventory 2.获取物料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 获取物料
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Array} data
     * @apiError  Error
     **/
    public function index()
    {
        $code = input('code');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->getReelsData($code, $this->getStorageUuid());
        switch ($result['status']) {
            case 0:
                $this->error('没有物料数据');
                break;
            case 1:
                $this->success('查询成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
                break;
        }
    }

    /**
     * @api {GET} /api/storage/inventory/seat_check 3.1检查站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkSeat
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 检验站位是否在盘点站位
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     * @apiParam {string} seatCode 站位号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Object} data
     * @apiSuccess {string} value seatCode
     * @apiError  Error
     **/
    public function checkSeat()
    {
        $code = input('code');
        $seatCode = input('seatCode');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->checkSeat($code, $seatCode, $this->getStorageUuid());
        switch ($result['status']) {
            case -2:
                $this->error('出库文件不存在');
                break;
            case -1:
                $this->error("{$seatCode}不属于盘点站位之中");
                break;
            case 1:
                $this->success('请求成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {GET} /api/storage/inventory/reels_check 3.2检查物料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkReels
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 检验物料是否在盘点物料中
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     * @apiParam {string} reelId reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Object} data
     * @apiSuccess {string} value reelId
     * @apiError  Error
     **/
    public function checkReels()
    {
        $code = input('code');
        $reelId = input('reelId');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->checkReels($code, $reelId, $this->getStorageUuid());
        switch ($result['status']) {
            case -2:
                $this->error('盘点文件不存在');
                break;
            case -1:
                $this->error("{$reelId}不属于盘点物料之中");
                break;
            case 1:
                $this->success('请求成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
        }
    }


    /**
     * @api {PUT} /api/storage/inventory/reels_confirm 4.更新数量
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  confirmReels
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 获取物料
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     * @apiParam {int} reelId reelId
     * @apiParam {int} updateCount 更新的数量
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Array} data
     * @apiError  Error
     **/
    public function confirmReels()
    {
        $code = input('code');
        $type = input('type');
        $reelId = input('reelId');
        $updateCount = input('updateCount');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->confirmReels($code, $reelId, $updateCount, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -1:
                $this->error('更新数量失败', $result['data']);
                break;
            case 0:
                $this->error('更新数量失败', $result['data']);
                break;
            case 1:
                $this->success('更新数量成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {DELETE} /api/storage/inventory/inventory_complete 5.1完成盘点
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  completeInventory
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 获取物料
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function completeInventory()
    {
        $code = input('code');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->complete($code, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -2:
                $this->error('盘点结算失败');
                break;
            case -1:
                $this->error('清除盘点占用失败');
                break;
            case 1:
                $this->success('盘点结算成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {DELETE} /api/storage/inventory/inventory_cancel 5.2取消盘点
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  cancelInventory
     * @apiGroup  inventory
     * @apiVersion 0.0.1
     * @apiDescription 获取物料
     *
     * @apiParam {string} code 货架号 | 容器号 | 物料号
     * @apiParam {int} type 盘点方式：货架1  容器2  物料3
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiError  Error
     **/
    public function cancelInventory()
    {
        $code = input('code');
        $type = input('type');
        $type = $this->judgeType($type);
        $service = InventoryFactory::getInventoryService($type);
        $result = $service->cancel($code, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -2:
                $this->error('盘点取消失败');
                break;
            case -1:
                $this->error('清除盘点占用失败');
                break;
            case 1:
                $this->success('盘点取消成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    private function judgeType($type)
    {
        if ($type == 1) {
            if ($this->getStorageType() == 1) {
                $type = 12;
            }
        }
        if ($type == 3) {
            if ($this->getStorageType() == 1) {
                $type = 32;
            }
        }
        return $type;
    }

}